package com.tencent.sr.iris.activity.dependency.dto.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardCommissionSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardFirstOrderSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardInviteSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardSprintSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.OperateBoardStatisticsDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.OperateSecondBoardDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.OperateThirdBoardDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.RankAwardConfigDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.SpecialStageListsDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.TaskPhaseDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.TaskUserJoinRecordDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.TaskWechatUserDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.UnJoinTaskDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.UserBonusRuleSpecialStageNow;
import com.tencent.sr.iris.activity.interfaces.request.operate.OperateBoardStatisticsRequest;
import com.tencent.sr.iris.activity.interfaces.request.operate.OperateBoardThirdRequest;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.OperateBoardCommissionResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.OperateBoardFirstOrderResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.OperateBoardInviteResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.OperateBoardSprintResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.OperateBoardStatisticsResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.OperateSecondBoardResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.OperateThirdBoardResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.SpecialStageListsResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.UnJoinTaskResponse;
import com.tencent.sr.iris.user.core.service.interfaces.dto.user.UserWideDTO;
import com.tencent.sr.rmall.common.request.PaginationRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.apache.commons.collections4.CollectionUtils;

public class OperateBoardUtils {

    public static OperateBoardSprintResponse toOperateBoardSprintResponse(AdvisorBoardSprintSecondDTO dto) {
        OperateBoardSprintResponse sprintResponse = new OperateBoardSprintResponse();
        sprintResponse.setTaskUserJoinRecordId(dto.getTaskUserJoinRecordId());
        sprintResponse.setAuditStatus(dto.getAuditStatus());
        sprintResponse.setAwardAmount(dto.getAwardAmount());
        sprintResponse.setPredictAwardAmount(dto.getPredictAwardAmount());
        sprintResponse.setTaskPhaseJoinTime(dto.getTaskPhaseJoinTime());
        sprintResponse.setValidGmv(dto.getValidGmv());
        sprintResponse.setValidGoodsNum(dto.getValidGoodsNum());
        sprintResponse.setAreaId(dto.getAreaId());

        if (Objects.nonNull(dto.getTaskPhase())) {
            sprintResponse.setTaskTargetEvaluation(dto.getTaskPhase().getTaskTargetEvaluation());
        }
        if (Objects.nonNull(dto.getTaskPhaseBonusRule())) {
            sprintResponse.setBonusRuleStage(dto.getTaskPhaseBonusRule().getBonusRuleStage());
            sprintResponse.setCommissionTimes(dto.getTaskPhaseBonusRule().getCommissionTimes());
        } else {
            sprintResponse.setBonusRuleStage(0);
        }
        if (Objects.nonNull(dto.getTaskWechatUser())) {
            sprintResponse.setIrisUidLevel(dto.getTaskWechatUser().getIrisUidLevel());
            sprintResponse.setIrisUid(dto.getTaskWechatUser().getIrisUid());
            sprintResponse.setWechatUserHeadimgurl(dto.getTaskWechatUser().getWechatUserHeadimgurl());
            sprintResponse.setWechatUserNickname(dto.getTaskWechatUser().getWechatUserNickname());
            sprintResponse.setWechatUserUnionid(dto.getTaskWechatUser().getWechatUserUnionid());
        }
        return sprintResponse;
    }

    public static OperateBoardInviteResponse toOperateBoardInviteResponse(AdvisorBoardInviteSecondDTO dto) {
        OperateBoardInviteResponse inviteResponse = new OperateBoardInviteResponse();
        inviteResponse.setTaskUserJoinRecordId(dto.getTaskUserJoinRecordId());
        inviteResponse.setAuditStatus(dto.getAuditStatus());
        inviteResponse.setAwardAmount(dto.getAwardAmount());
        if (Objects.nonNull(dto.getTaskPhase())) {
            inviteResponse.setTaskTargetEvaluation(dto.getTaskPhase().getTaskTargetEvaluation());
        }
        if (Objects.nonNull(dto.getTaskPhaseBonusRule())) {
            inviteResponse.setBonusRuleStage(dto.getTaskPhaseBonusRule().getBonusRuleStage());
            inviteResponse.setCommissionTimes(dto.getTaskPhaseBonusRule().getCommissionTimes());
        } else {
            inviteResponse.setBonusRuleStage(0);
        }
        inviteResponse.setPredictAwardAmount(dto.getPredictAwardAmount());
        inviteResponse.setTaskPhaseJoinTime(dto.getTaskPhaseJoinTime());
        inviteResponse.setTotalInviteUserNum(dto.getTotalInviteUserNum());
        inviteResponse.setValidInviteUserNum(dto.getValidInviteUserNum());
        inviteResponse.setAreaId(dto.getAreaId());
        if (Objects.nonNull(dto.getTaskWechatUser())) {
            inviteResponse.setWechatUserHeadimgurl(dto.getTaskWechatUser().getWechatUserHeadimgurl());
            inviteResponse.setWechatUserNickname(dto.getTaskWechatUser().getWechatUserNickname());
            inviteResponse.setWechatUserUnionid(dto.getTaskWechatUser().getWechatUserUnionid());
            inviteResponse.setIrisUid(dto.getTaskWechatUser().getIrisUid());
            inviteResponse.setIrisUidLevel(dto.getTaskWechatUser().getIrisUidLevel());
        }
        return inviteResponse;
    }


    public static OperateBoardCommissionResponse toOperateBoardCommissionResponse(AdvisorBoardCommissionSecondDTO dto) {
        OperateBoardCommissionResponse response = new OperateBoardCommissionResponse();
        response.setTaskUserJoinRecordId(dto.getTaskUserJoinRecordId());
        response.setAwardAmount(dto.getAwardAmount());
        response.setPredictAwardAmount(dto.getPredictAwardAmount());
        response.setAuditStatus(dto.getAuditStatus());

        TaskPhaseDTO taskPhase = Optional.ofNullable(dto.getTaskPhase()).orElseGet(TaskPhaseDTO::new);
        response.setTaskPhaseStartTime(taskPhase.getTaskPhaseStartTime());
        response.setTaskPhaseStatus(taskPhase.getTaskPhaseStatus());

        response.setValidOrderNum(dto.getValidOrderNum());
        response.setPredictValidOrderNum(dto.getPredictValidOrderNum());

        response.setAreaId(dto.getAreaId());

        TaskWechatUserDTO taskWechatUser = Optional.ofNullable(dto.getTaskWechatUser())
                .orElseGet(TaskWechatUserDTO::new);
        response.setIrisUid(taskWechatUser.getIrisUid());
        response.setIrisUidLevel(taskWechatUser.getIrisUidLevel());
        response.setWechatUserUnionid(taskWechatUser.getWechatUserUnionid());
        response.setWechatUserNickname(taskWechatUser.getWechatUserNickname());
        response.setWechatUserHeadimgurl(taskWechatUser.getWechatUserHeadimgurl());
        return response;
    }

    public static OperateBoardFirstOrderResponse toOperateFirstOrderSecondResponse(
            AdvisorBoardFirstOrderSecondDTO dto) {
        OperateBoardFirstOrderResponse response = new OperateBoardFirstOrderResponse();
        response.setAuditStatus(dto.getAuditStatus());
        response.setAwardAmount(dto.getAwardAmount());
        response.setTaskUserJoinRecordId(dto.getTaskUserJoinRecordId());
        if (Objects.nonNull(dto.getTaskPhaseBonusRule())) {
            response.setBonusRuleStage(dto.getTaskPhaseBonusRule().getBonusRuleStage());
            response.setCommissionTimes(dto.getTaskPhaseBonusRule().getCommissionTimes());
        } else {
            response.setBonusRuleStage(0);
        }
        if (Objects.nonNull(dto.getTaskPhase())) {
            response.setTaskTargetEvaluation(dto.getTaskPhase().getTaskTargetEvaluation());
        }
        response.setTaskPhaseJoinTime(dto.getTaskPhaseJoinTime());
        response.setPredictAwardAmount(dto.getPredictAwardAmount());
        response.setValidInviteUserNum(dto.getValidInviteUserNum());
        response.setTotalInviteUserNum(dto.getTotalInviteUserNum());
        response.setAreaId(dto.getAreaId());
        if (Objects.nonNull(dto.getTaskWechatUser())) {
            response.setWechatUserNickname(dto.getTaskWechatUser().getWechatUserNickname());
            response.setWechatUserHeadimgurl(dto.getTaskWechatUser().getWechatUserHeadimgurl());
            response.setIrisUid(dto.getTaskWechatUser().getIrisUid());
            response.setWechatUserUnionid(dto.getTaskWechatUser().getWechatUserUnionid());
            response.setIrisUidLevel(dto.getTaskWechatUser().getIrisUidLevel());
        }
        return response;
    }

    public static OperateSecondBoardResponse toOperateSecondBoardResponse(OperateSecondBoardDTO dto) {
        OperateSecondBoardResponse response = new OperateSecondBoardResponse();
        response.setAuditStatus(dto.getAuditStatus());
        response.setAwardAmount(dto.getAwardAmount());
        response.setTaskUserJoinRecordId(dto.getTaskUserJoinRecordId());
        if (Objects.nonNull(dto.getTaskPhaseBonusRule())) {
            response.setBonusRuleStage(dto.getTaskPhaseBonusRule().getBonusRuleStage());
            response.setCommissionTimes(dto.getTaskPhaseBonusRule().getCommissionTimes());
        } else {
            response.setBonusRuleStage(0);
        }
        if (Objects.nonNull(dto.getTaskPhase())) {
            response.setTaskTargetEvaluation(dto.getTaskPhase().getTaskTargetEvaluation());
        }
        response.setTaskPhaseJoinTime(dto.getTaskPhaseJoinTime());
        response.setPredictAwardAmount(dto.getPredictAwardAmount());
        response.setValidInviteUserNum(dto.getValidInviteUserNum());
        response.setTotalInviteUserNum(dto.getTotalInviteUserNum());
        response.setPredictValidOrderNum(dto.getPredictValidOrderNum());
        response.setValidOrderNum(dto.getValidOrderNum());

        TaskPhaseDTO taskPhase = Optional.ofNullable(dto.getTaskPhase()).orElseGet(TaskPhaseDTO::new);
        response.setTaskPhaseStartTime(taskPhase.getTaskPhaseStartTime());
        response.setTaskPhaseStatus(taskPhase.getTaskPhaseStatus());

        response.setValidGmv(dto.getValidGmv());
        response.setValidGoodsNum(dto.getValidGoodsNum());

        response.setAreaId(dto.getAreaId());
        if (Objects.nonNull(dto.getTaskWechatUser())) {
            response.setWechatUserNickname(dto.getTaskWechatUser().getWechatUserNickname());
            response.setWechatUserHeadimgurl(dto.getTaskWechatUser().getWechatUserHeadimgurl());
            response.setIrisUid(dto.getTaskWechatUser().getIrisUid());
            response.setWechatUserUnionid(dto.getTaskWechatUser().getWechatUserUnionid());
            response.setIrisUidLevel(dto.getTaskWechatUser().getIrisUidLevel());
        }

        response.setCountPromoteWechatUserNum(dto.getCountPromoteWechatUserNum());
        response.setCountPromoteSeniorWechatUserNum(dto.getCountPromoteSeniorWechatUserNum());
        response.setCountPromoteSuperWechatUserNum(dto.getCountPromoteSuperWechatUserNum());
        response.setCountPromoteWechatUserNumContributing(dto.getCountPromoteWechatUserNumContributing());
        response.setSumMonthPredictAwardAmount(dto.getSumMonthPredictAwardAmount());
        response.setSumPromoteCommissionFee(dto.getSumPromoteCommissionFee());
        response.setSumMonthAwardAmount(dto.getSumMonthAwardAmount());
        response.setSumMonthValidGmv(dto.getSumMonthValidGmv());
        response.setSumValidGmv(dto.getSumValidGmv());

        //排行榜
        response.setAwardType(dto.getAwardType());
        response.setRankNum(dto.getRankNum());
        response.setValidCommissionBillCommissionFee(dto.getValidCommissionBillCommissionFee());
        RankAwardConfigDTO rankAwardConfigDTO = Optional.ofNullable(dto.getRankAwardConfig())
                .orElseGet(RankAwardConfigDTO::new);
        response.setCustomAwardName(rankAwardConfigDTO.getCustomAwardName());
        if (Objects.nonNull(rankAwardConfigDTO.getRankingStart())
                && Objects.nonNull(rankAwardConfigDTO.getRankingEnd())) {
            if (rankAwardConfigDTO.getRankingStart().equals(rankAwardConfigDTO.getRankingEnd())) {
                response.setRankRang(rankAwardConfigDTO.getRankingStart().toString());
            } else {
                response.setRankRang(rankAwardConfigDTO.getRankingStart() + "-" + rankAwardConfigDTO.getRankingEnd());
            }
        }

        //团推团
        response.setSumMonthValidCommissionBillCommissionFee(dto.getSumMonthValidCommissionBillCommissionFee());
        response.setSumValidCommissionBillCommissionFee(dto.getSumValidCommissionBillCommissionFee());

        response.setAdvisorUid(dto.getAdvisorUid());

        //特邀会员冲刺赛
        response.setUserLevel(dto.getUserLevel());
        response.setStageNum(dto.getStageNum());
        response.setTaskUserCyclePerformanceId(dto.getTaskUserCyclePerformanceId());
        TaskUserJoinRecordDTO taskUserJoinRecordDTO = dto.getTaskUserJoinRecordDTO();
        if (Objects.nonNull(taskUserJoinRecordDTO)) {
            response.setAdvisorUid(taskUserJoinRecordDTO.getAdvisorUid());
            response.setAreaId(taskUserJoinRecordDTO.getArea());
            response.setTaskPhaseJoinTime(taskUserJoinRecordDTO.getTaskPhaseJoinTime());
            response.setTotalAwardAmount(taskUserJoinRecordDTO.getAwardAmount());
            response.setTotalValidCommissionBillCommissionFee(
                    taskUserJoinRecordDTO.getValidCommissionBillCommissionFee());
            response.setTotalValidGmv(taskUserJoinRecordDTO.getValidGmv());
            response.setAwardAmount(taskUserJoinRecordDTO.getAwardAmount());
        }
        return response;
    }

    public static OperateThirdBoardResponse toOperateThirdBoardResponse(
            OperateThirdBoardDTO dto) {
        OperateThirdBoardResponse response = new OperateThirdBoardResponse();
        response.setCommissionStatus(dto.getCommissionStatus());
        response.setOrderStatus(dto.getOrderStatus());
        response.setPlainOrderId(dto.getPlainOrderId());
        response.setThirdPartOrderPrice(dto.getThirdPartOrderPrice());

        response.setCommissionFee(dto.getCommissionFee());
        response.setPredictCommissionFee(dto.getPredictCommissionFee());
        response.setCompensateCommissionFee(dto.getCompensateCommissionFee());
        response.setPredictCompensateCommissionFee(dto.getPredictCompensateCommissionFee());
        response.setPredictCommissionTotalFee(dto.getPredictCommissionTotalFee());

        response.setCompensateCommissionProvideStatus(dto.getCompensateCommissionProvideStatus());
        response.setThirdPartOrderCreateTime(dto.getThirdPartOrderCreateTime());

        TaskWechatUserDTO taskWechatUser = dto.getTaskWechatUser();
        if (Objects.isNull(taskWechatUser)) {
            taskWechatUser = dto.getWechatUser();
        }
        if (Objects.isNull(taskWechatUser) && Objects.nonNull(dto.getTaskUserJoinRecord())) {
            TaskUserJoinRecordDTO joinRecord = dto.getTaskUserJoinRecord();
            taskWechatUser = Optional.ofNullable(joinRecord.getWechatUserDTO()).orElseGet(TaskWechatUserDTO::new);
        }
        taskWechatUser = Optional.ofNullable(taskWechatUser).orElseGet(TaskWechatUserDTO::new);
        response.setIrisUid(taskWechatUser.getIrisUid());
        response.setWechatUserNickname(taskWechatUser.getWechatUserNickname());
        response.setWechatUserHeadimgurl(taskWechatUser.getWechatUserHeadimgurl());
        response.setWechatUserUnionid(taskWechatUser.getWechatUserUnionid());

        response.setPromoteStatus(dto.getPromoteStatus());
        response.setPromoteUserInviteDatetime(dto.getPromoteUserInviteDatetime());
        response.setPromoteStart(dto.getPromoteStart());
        response.setPromoteEnd(dto.getPromoteEnd());
        response.setPromoteGmv(dto.getPromoteGmv());
        response.setSumMonthValidGmv(dto.getSumMonthValidGmv());
        response.setSumPromoteGmv(dto.getSumPromoteGmv());
        response.setSumPredictCommissionFee(dto.getSumPredictCommissionFee());

        UserBonusRuleSpecialStageNow userBonusRuleSpecialStageNow = Optional.ofNullable(
                dto.getUserBonusRuleSpecialStageNow())
                .orElseGet(UserBonusRuleSpecialStageNow::new);
        response.setDayStart(userBonusRuleSpecialStageNow.getDayStart());
        response.setDayEnd(userBonusRuleSpecialStageNow.getDayEnd());
        //排行榜新加
        response.setIsInLeaderboard(dto.getIsInLeaderboard());
        response.setNotInLeaderboarReason(dto.getNotInLeaderboarReason());
        response.setOrderTime(dto.getOrderTime());
        response.setOrderEndStatus(dto.getOrderEndStatus());
        response.setOrderAmount(dto.getOrderAmount());
        response.setCategoryLevel3Id(dto.getCategoryLevel3Id());
        response.setUserPredictCommissionFee(dto.getUserPredictCommissionFee());
        response.setPredictCommissionFee(dto.getPredictCommissionFee());
        response.setUserLevelSnapshot(dto.getUserLevelSnapshot());

        //团推团
        response.setSumMonthValidCommissionBillCommissionFee(dto.getSumMonthValidCommissionBillCommissionFee());
        response.setSumValidCommissionBillCommissionFee(dto.getSumValidCommissionBillCommissionFee());
        response.setValidCommissionBillCommissionFee(dto.getValidCommissionBillCommissionFee());

        //特邀会员冲刺赛
        response.setIsValid(dto.getIsValid());
        response.setValidGmv(dto.getValidGmv());
        response.setCommissionBillCommissionFee(dto.getCommissionBillCommissionFee());
        if (Objects.nonNull(dto.getMCategoryLevel3Id())) {
            response.setCategoryLevel3Id(dto.getMCategoryLevel3Id());
        }
        return response;
    }

    public static UnJoinTaskResponse toUnJoinTaskResponse(UnJoinTaskDTO dto) {
        UnJoinTaskResponse response = new UnJoinTaskResponse();
        response.setUid(dto.getUserUid());
        response.setTutorUid(dto.getTutorUid());
        return response;
    }

    public static String toOperateBoardStaticRequest(OperateBoardStatisticsRequest request) {
        JSONObject jsonObject = (JSONObject) JSON.toJSON(request);
        jsonObject.put("task_phase_start_time", GroupTaskBoardUtils.timestampToTime(request.getStartTime()));
        jsonObject.put("task_phase_end_time", GroupTaskBoardUtils.timestampToTime(request.getEndTime()));
        return jsonObject.toJSONString();
    }

    public static OperateBoardStatisticsResponse toOperateBoardStaticResponse(OperateBoardStatisticsDTO dto) {
        OperateBoardStatisticsResponse response = new OperateBoardStatisticsResponse();
        response.setCountTaskPhaseOnline(dto.getCountTaskPhaseOnline());
        response.setCountTaskPhaseEnded(dto.getCountTaskPhaseEnded());
        response.setCountJoinRecordWechatUserNum(dto.getCountJoinRecordWechatUserNum());
        response.setSumAwardAmount(dto.getSumAwardAmount());
        response.setSumPredictAwardAmount(dto.getSumPredictAwardAmount());
        return response;
    }

    public static SpecialStageListsResponse toSpecialStageListsResponse(SpecialStageListsDTO dto) {
        SpecialStageListsResponse response = new SpecialStageListsResponse();
        response.setTaskPhaseId(dto.getTaskPhaseId());
        response.setRuleStageNum(dto.getRuleStageNum());
        response.setDayStart(dto.getDayStart());
        response.setDayEnd(dto.getDayEnd());
        return response;
    }

    public static void handleUserInfo(UnJoinTaskResponse response, Map<String, UserWideDTO> wideDTOMap) {

        UserWideDTO userWideDTO = Optional.ofNullable(wideDTOMap.get(response.getUid())).orElseGet(UserWideDTO::new);
        response.setNickName(userWideDTO.getNickName());
        response.setUserLevel(userWideDTO.getLevelId());
        List<Long> groupChain = userWideDTO.getGroupChain();
        if (CollectionUtils.isNotEmpty(groupChain) && groupChain.size() >= 2) {
            //groupChain第二个为大区id
            response.setAreaId(groupChain.get(1));
        }

        UserWideDTO tutorWideDTO = Optional.ofNullable(wideDTOMap.get(response.getTutorUid()))
                .orElseGet(UserWideDTO::new);
        response.setTutorNickName(tutorWideDTO.getNickName());
    }

    public static String toThirdBoardBody(PaginationRequest<OperateBoardThirdRequest> request) {
        JSONObject jsonObject = GroupTaskBoardUtils.toJsonString(request);
        jsonObject.put("third_part_order_create_start_time",
                GroupTaskBoardUtils.timestampToTime(request.getQueryCriteria().getStartTime()));
        jsonObject.put("third_part_order_create_end_time",
                GroupTaskBoardUtils.timestampToTime(request.getQueryCriteria().getEndTime()));
        jsonObject.put("promote_user_invite_start_datetime",
                GroupTaskBoardUtils.timestampToTime(request.getQueryCriteria().getStartTime()));
        jsonObject.put("promote_user_invite_end_datetime",
                GroupTaskBoardUtils.timestampToTime(request.getQueryCriteria().getEndTime()));
        String reqBody = jsonObject.toJSONString();
        return reqBody;
    }
}
